//
//  PAWithdrawalsView.m
//  PAMobileWallet
//
//  Created by Pay_SMP002 on 13-7-4.
//  Copyright (c) 2013年 平安付. All rights reserved.
//

#import "PAWithdrawalsView.h"
#import "PAWithdrawalsViewController.h"

#import "ColorTools.h"

@implementation PAWithdrawalsView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        CGFloat x, y, w, h;
        CGSize size = frame.size;
        CGFloat space = 10;


        x = y = 0;
        h = 44;
        w = WIDTH_PHONE;

        titleBar = [[PATitleBar alloc] initWithFrame:CGRectMake(x, y, w, h)];
        titleBar.parentController = self;
        [titleBar setBackgroundImage:[UIImage imageNamed:@"public_navbg.png"]];
        [titleBar setLeftButtonImage:[UIImage imageNamed:@"public_navbackBT.png"] right:nil];
        [titleBar setTitle:@"提现"];
        [self addSubview:titleBar];

        y = CGRectGetMaxY(titleBar.frame);
        h = HEIGHT_CURRENT - y;
        scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        scrollView.userInteractionEnabled = YES;
        scrollView.scrollEnabled = NO;
        [self addSubview:scrollView];

        y = space;
        w = 215;
        h = 115;
        x = (size.width - w) / 2;
        bankCard = [[PABankCard alloc] initWithFrame:CGRectMake(x, y, w, h)];
        bankCard.parentController = self;
        [bankCard setMode:BANKCARDMODE_Defalut];
        [bankCard setBankCardImage:[UIImage imageNamed:@"card_card1.png"]];
        [scrollView addSubview:bankCard];

        y = CGRectGetMaxY(bankCard.frame) + space;
        w = 280;
        h = 33;
        x = (size.width - w) / 2;
        nameText = [[UITextField alloc] initWithFrame:CGRectMake(x, y, w, h)];
        nameText.borderStyle = UITextBorderStyleRoundedRect;
        nameText.placeholder = @"姓名";
        nameText.delegate = self;
        [scrollView addSubview:nameText];

        y = CGRectGetMaxY(nameText.frame);
        h = 33;
        w = 100;
        x = CGRectGetMinX(nameText.frame);
        UILabel *balanceDes = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
        balanceDes.backgroundColor = [UIColor clearColor];
        balanceDes.textColor = [UIColor grayColor];
        balanceDes.adjustsFontSizeToFitWidth = YES;
        balanceDes.font = [UIFont systemFontOfSize:14];
        balanceDes.textAlignment = UITextAlignmentRight;
        balanceDes.text = @"可提现余额: ";
        [scrollView addSubview:balanceDes];

        x = CGRectGetMaxX(balanceDes.frame);
        balanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
        balanceLabel.backgroundColor = [UIColor clearColor];
        balanceLabel.textAlignment = UITextAlignmentLeft;
        balanceLabel.font = [UIFont systemFontOfSize:18];
        balanceLabel.textColor = _RGB16(0xF6946D, 1.0);
        balanceLabel.text = @"600.00元";
        [scrollView addSubview:balanceLabel];


        y = CGRectGetMaxY(balanceLabel.frame) + space;
        w = 280;
        x = (WIDTH_PHONE - w) / 2;
        moneyText = [[UITextField alloc] initWithFrame:CGRectMake(x, y, w, h)];
        moneyText.borderStyle = UITextBorderStyleRoundedRect;
        moneyText.placeholder = @"提现金额";
        moneyText.delegate = self;
        [scrollView addSubview:moneyText];

        y = CGRectGetMaxY(moneyText.frame);
        moneyDes =  [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
        moneyDes.backgroundColor = [UIColor clearColor];
        moneyDes.textColor = [UIColor grayColor];
        moneyDes.adjustsFontSizeToFitWidth = YES;
        moneyDes.font = [UIFont systemFontOfSize:14];
        moneyDes.textAlignment = UITextAlignmentLeft;
        moneyDes.text = @"每笔限额49999.00元，本日可提现3次。";
        [scrollView addSubview:moneyDes];

        y = CGRectGetMaxY(moneyDes.frame) + space;
        payPwdText = [[UITextField alloc] initWithFrame:CGRectMake(x, y, w, h)];
        payPwdText.delegate = self;
        payPwdText.borderStyle = UITextBorderStyleRoundedRect;
        payPwdText.placeholder = @"支付密码";
        payPwdText.secureTextEntry = YES;
        payPwdText.returnKeyType = UIReturnKeyDone;
        [scrollView addSubview:payPwdText];

        y = CGRectGetMaxY(payPwdText.frame) + space * 2;
        w = 289;
        h = 49;
        x = (WIDTH_PHONE - w) / 2;
        confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        confirmButton.frame = CGRectMake(x, y, w, h);
        [confirmButton setBackgroundImage:[UIImage imageNamed:@"card_blank.png"] forState:UIControlStateNormal];
        [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
        [confirmButton addTarget:self action:@selector(buttonActive:) forControlEvents:UIControlEventTouchUpInside];
        confirmButton.tag = 0;
        [scrollView addSubview:confirmButton];
    }
    return self;
}

-(void)buttonActive:(id)sender {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                    message:@"您已成功申请提现。请在1-2个工作日后查询储蓄卡"
                                                   delegate:self
                                          cancelButtonTitle:nil
                                          otherButtonTitles:@"确定", nil];

    [alert show];
}

- (void)titleBarCallBack:(int)index {
    if (index == 0) {
        [self.parentController back];
    }
}

#pragma mark -
#pragma mark ========================================
#pragma mark UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        [self.parentController goHome];
    }
}

#pragma mark -
#pragma mark ========================================
#pragma mark UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    CGFloat offset = 0;
    if (textField == nameText) {
        offset = 0;
    }
    else if (textField == moneyText) {
        offset = 60;
    }
    else if (textField == payPwdText) {
        offset = 140;
    }
    [scrollView setContentOffset:CGPointMake(0, offset)];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
    [scrollView setContentOffset:CGPointMake(0, 0)];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    if (textField == nameText) {
        [moneyText becomeFirstResponder];
    }
    else if (textField == moneyText) {
        [payPwdText becomeFirstResponder];
    }
    else if (textField == payPwdText) {
        [textField resignFirstResponder];
    }

    return YES;
}

@end
